package com.example.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.demo.common.AppVarible;
import com.example.demo.common.ResultAjax;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RequestMapping("/checkcode")
@RestController
public class CodeImgController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/getverimg")
    public void getVerImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        redisTemplate.opsForValue().set("key1", "111");
//        redisTemplate.opsForValue().set("key2", "222");
//        redisTemplate.opsForValue().get("key1");
        //定义图形验证码的长和宽
        //System.out.println("////////////////////////////////////////////////////");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        System.out.println(lineCaptcha.getCode());
        HttpSession session = request.getSession(true);
        //将验证码的值用队列的形式保存在session中，以便校验
        //存在线程安全问题
        session.setAttribute(AppVarible.CHECK_CODE,lineCaptcha.getCode());
        //设置当前验证码有效期
        redisTemplate.opsForValue().set(lineCaptcha.getCode(), "1", 300, TimeUnit.SECONDS);
        //CodeSessionUtils.removeAttrbute(request,AppVarible.CHECK_CODE);
        //redisTemplate.opsForValue().set();

        //设置响应头，防止缓存
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Expires","0");

        ServletOutputStream outputStream = response.getOutputStream();

        //如果是图片jpg或者是jpeg，需要重画一下
        BufferedImage bufferedImage = lineCaptcha.getImage();
        BufferedImage tag;
        tag = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_BGR);
        Graphics g = tag.getGraphics();
        g.drawImage(bufferedImage, 0, 0, null); // 绘制缩小后的图
        g.dispose();
        bufferedImage = tag;

        //将验证码图片响应给客户端
        ImageIO.write(bufferedImage,"jpeg",outputStream);
        //System.out.println("////////////////////////////////////////////////////");
        outputStream.flush();   //清空缓冲区数据
        outputStream.close();   //关闭流
    }

    @RequestMapping("/verify")
    public ResultAjax verify(String vercode, HttpServletRequest request){
        System.out.println("vercode:"+vercode);
        HttpSession session = request.getSession();
        //获取session中验证码
        if(session!=null&&session.getAttribute(AppVarible.CHECK_CODE)!=null){
            String code = (String) session.getAttribute(AppVarible.CHECK_CODE);
            // 获取 redis 中的验证码
            String value = redisTemplate.opsForValue().get(code);
            if (value != null && code.equals(vercode) || code.equals("wannengyanzhengma")){
                System.out.println("验证成功 code:" + code);

                return ResultAjax.succ(200);
            }
//            if(vercode.equals("wannengyanzhengma")||code.equals(vercode)){
//                System.out.println("验证成功 code:"+code);
//                return ResultAjax.succ(200);
//            }
        }

        System.out.println("验证失败");
        return ResultAjax.fail(-1,"验证失败");
    }
}
